package com.woniu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.pojo.Class;
import com.woniu.pojo.Dept;
import com.woniu.pojo.Emp;
import com.woniu.pojo.Major;
import com.woniu.service.ClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("class")
public class ClassController {
    @Autowired
    ClassService classService;

    /*增加跳转*/
    @RequestMapping("addIndex")
    public String addIndex(){
        return "WEB-INF/class/add";
    }
    /*增加*/
    @RequestMapping("add")
    @ResponseBody
    public Map<String,Object> add(Class clazz){
        Map<String,Object> map = new HashMap<>();
        try {
            classService.add(clazz);
            map.put("result",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result",false);
        }
        return map;
    }
    /*删除*/
    @RequestMapping("deleteById")
    @ResponseBody
    public Map<String,Object> deleteById(Integer id){
        Map<String,Object> map = new HashMap<>();
        try {
            classService.deleteById(id);
            map.put("result",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result",false);
        }
        return map;
    }
    /*批量删除*/
    @RequestMapping("deleteByIds")
    @ResponseBody
    public Map<String,Object> deleteByIds(Integer[] ids){
        Map<String,Object> map = new HashMap<>();
        try {
            if(ids!=null){
                classService.deleteByIds(ids);
                map.put("result",true);
            }else {
                map.put("result","empty");
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result",false);
        }
        return map;
    }
    /*编辑跳转*/
    @RequestMapping("edit")
    public String edit(Model model, Integer id){
        model.addAttribute("id",id);
        return "WEB-INF/class/edit";
    }
    /*查单个*/
    @RequestMapping("findOne")
    @ResponseBody
    public Map<String,Object> findOne(Integer id){
        Map<String,Object> map = new HashMap<>();
        try {
            List<Major> majorList = classService.selectMajorAll();
            map.put("majorList",majorList);
            List<Emp> empList = classService.selectEmpByBZR();
            map.put("empList",empList);
            List<Dept> deptList = classService.selectDeptAll();
            map.put("deptList",deptList);
            Class clazz =classService.findOne(id);
            map.put("class",clazz);
            map.put("result",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result",false);
        }
        return map;
    }
    /*改变*/
    @RequestMapping("updateById")
    @ResponseBody
    public Map<String,Object> updateById(Class clazz){
        Map<String,Object> map = new HashMap<>();
        try {
            classService.updateById(clazz);
            map.put("result",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result",false);
        }
        return map;
    }
    /* 查询跳转*/
    @RequestMapping("index")
    public String index(){
        return "WEB-INF/class/index";
    }

    /*查询，按条件---*/
    @RequestMapping("selectByExample")
    @ResponseBody
    public Map<String,Object> selectByExample(Class clazz,@RequestParam(name="pageNum",defaultValue = "1")
            Integer pageNum,@RequestParam(name = "pageSize",defaultValue = "3")
                                                      Integer pageSize){
        Map<String,Object> map = new HashMap<>();
        try {
            PageHelper.startPage(pageNum,pageSize);
            List<Class> classList = classService.selectByExample(clazz);
            PageInfo<Class> pageInfo = new PageInfo<>(classList);
            map.put("classList",classList);
            map.put("pageInfo",pageInfo);
            map.put("result",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result",false);
        }
        return map;
    }


}
